package org.jeecg.modules.tencent_esign.tencent_upload.controller;

import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.modules.tencent_esign.tencent_upload.service.ITencentUploadService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import javax.servlet.http.HttpServletRequest;

/**
 * @Description: 腾讯电子签文件上传
 * @Author: jeecg-boot
 * @Date: 2025-06-28
 * @Version: V1.0
 */
@Api(tags = "腾讯电子签文件上传")
@RestController
@RequestMapping("/tencent_esign/upload")
@Slf4j
public class TencentUploadController {

    @Autowired
    private ITencentUploadService tencentUploadService;

    /**
     * 上传文件到腾讯电子签平台
     *
     * @param file 文件
     * @param request 请求
     * @return
     */
    @ApiOperation(value = "上传文件到腾讯电子签平台", notes = "上传文件到腾讯电子签平台")
    @PostMapping(value = "/uploadFile")
    public Result<?> uploadFile(@RequestParam(name = "file", required = false) MultipartFile file,
                                HttpServletRequest request) {
        try {
            String fileType = request.getParameter("fileType");
            String fileName = request.getParameter("fileName");
            String businessType = request.getParameter("businessType");
            
            // 参数说明：
            // fileType: 文件类型，用于内部处理，如PDF、WORD等
            // fileName: 文件名，可选参数，如果不传则使用原始文件名
            // businessType: 业务类型，可选参数，支持 TEMPLATE(模板)、DOCUMENT(文档)、SEAL(印章)，默认为DOCUMENT
            
            return tencentUploadService.uploadFile(file, fileType, fileName);
        } catch (Exception e) {
            log.error("上传文件到腾讯电子签平台失败", e);
            return Result.error("上传失败：" + e.getMessage());
        }
    }
    
    /**
     * 上传文件到腾讯电子签平台（完整参数版本）
     *
     * @param file 文件
     * @param businessType 业务类型 (TEMPLATE: 模板, DOCUMENT: 文档, SEAL: 印章)
     * @param fileName 文件名
     * @param agentAppId 渠道应用标识
     * @param proxyOrganizationOpenId 第三方平台子客企业标识
     * @param proxyOperatorOpenId 第三方平台子客企业中的员工标识
     * @return
     */
    @ApiOperation(value = "上传文件到腾讯电子签平台（完整参数）", notes = "上传文件到腾讯电子签平台（完整参数）")
    @PostMapping(value = "/uploadFileFull")
    public Result<?> uploadFileFull(@RequestParam(name = "file", required = false) MultipartFile file,
                                    @RequestParam(name = "businessType", required = false, defaultValue = "DOCUMENT") String businessType,
                                    @RequestParam(name = "fileName", required = false) String fileName,
                                    @RequestParam(name = "agentAppId") String agentAppId,
                                    @RequestParam(name = "proxyOrganizationOpenId", required = false) String proxyOrganizationOpenId,
                                    @RequestParam(name = "proxyOperatorOpenId") String proxyOperatorOpenId) {
        try {
            // 构造Agent参数
            com.alibaba.fastjson.JSONObject agent = new com.alibaba.fastjson.JSONObject();
            agent.put("AppId", agentAppId);
            
            if (proxyOrganizationOpenId != null && !proxyOrganizationOpenId.isEmpty()) {
                agent.put("ProxyOrganizationOpenId", proxyOrganizationOpenId);
            }
            
            com.alibaba.fastjson.JSONObject proxyOperator = new com.alibaba.fastjson.JSONObject();
            proxyOperator.put("OpenId", proxyOperatorOpenId);
            agent.put("ProxyOperator", proxyOperator);
            
            // 文件类型参数（用于内部处理）
            String fileType = null;
            if (fileName != null) {
                int dotIndex = fileName.lastIndexOf(".");
                if (dotIndex > 0 && dotIndex < fileName.length() - 1) {
                    fileType = fileName.substring(dotIndex + 1).toUpperCase();
                }
            }
            
            return tencentUploadService.uploadFile(file, fileType, fileName, businessType, agent);
        } catch (Exception e) {
            log.error("上传文件到腾讯电子签平台失败", e);
            return Result.error("上传失败：" + e.getMessage());
        }
    }
}